//
//  YZMTrendCell.m
//  YZMicroStock
//
//  Created by apple on 17/3/7.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMTrendCell.h"
#import "NSNumber+Utils.h"
#import "YZMTimeJudge.h"

@interface YZMTrendCell ()
@property(nonatomic, strong) UIView *circleView;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UILabel *nameLabel;
@property(nonatomic, strong) UILabel *detailLabel;
@property(nonatomic, strong) UILabel *timeLabel;
@property(nonatomic, strong) UIButton *btnTrade;

@end

@implementation YZMTrendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createObjects];
        self.rightMargin = 10.0;
        self.leftMargin = 10.0;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

- (void)createObjects{
    
//    self.circleView = [UIView new];
//    [self.contentView addSubview:self.circleView];
//    [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.width.mas_equalTo(26);
//        make.top.equalTo(self.contentView).offset(19);
//        make.left.equalTo(self.contentView).offset(27);
//    }];
//
//    self.circleView.layer.cornerRadius = 13.0;
//    self.circleView.backgroundColor = [YZMAppStyle colorForBlueText];
    
//    self.titleLabel = [YZMAppStyle labelWithSmallWhite];
//    [self.contentView addSubview:self.titleLabel];
//    self.titleLabel.text = @"买";
//    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.equalTo(self.circleView);
//    }];
    
    self.nameLabel = [YZMAppStyle labelWithMediumWhite];
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(14);
        make.left.equalTo(self).offset(20);
    }];
    
    self.detailLabel = [YZMAppStyle labelWithSmallLightGray];
    [self.contentView addSubview:self.detailLabel];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(8);
    }];
    UIButton *btnTap = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:btnTap];
    [btnTap mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self.contentView).offset(-10);
        make.width.equalTo(@60);
        make.height.equalTo(@30);
    }];
    [btnTap addTarget:self action:@selector(trade:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btnTrade = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnTrade.userInteractionEnabled = NO;
    self.btnTrade.titleLabel.font = [YZMAppStyle smallSizeFont];
    [self.btnTrade setTitle:@"点买" forState:UIControlStateNormal];
    [self.btnTrade setTitleColor:[YZMAppStyle colorForYellowButton] forState:UIControlStateNormal];
    [self.contentView addSubview:self.btnTrade];
    [self.btnTrade mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.width.equalTo(@40);
        if (ScreenWidth == 320) {
            make.right.equalTo(self.contentView).offset(-10);
            make.height.equalTo(@20);
        }else{
            make.right.equalTo(self.contentView).offset(-20);
            make.height.equalTo(@25);
        }
       
    }];
    [YZMSetBorderAndCornerRadius setView:self.btnTrade borderWidth:1.0 borderColor:[YZMAppStyle colorForYellowButton] cornerRadius:4.0];
    
    self.timeLabel = [YZMAppStyle labelWithSmallLightGray];
    [self.contentView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.nameLabel);
        make.centerY.equalTo(self);
        make.right.equalTo(self.contentView).offset(-80);
    }];
    
    
}

- (void)trade:(UIButton *)btn{
    if (self.handle) {
        self.handle();
    }
}

-(void)setNumberForColor:(NSInteger)numberForColor{
    
    _numberForColor = numberForColor;
    NSInteger colorIndex = numberForColor % 3;
    self.circleView.backgroundColor = [self circleViewBGColorIndex:colorIndex];
    
}

-(UIColor *)circleViewBGColorIndex:(NSInteger )index{
    
    NSArray *colorArray = @[[YZMAppStyle colorForRedMark],[YZMAppStyle trendColorSecond],[YZMAppStyle trendColorThird]];
    return colorArray[index];
}

-(void)setTrendModel:(YZMHomeTrendModel *)trendModel{
    
    _trendModel = trendModel;
    _nameLabel.text = trendModel.username;
    
    NSString *detailString = [NSString stringWithFormat:@"%@元点买了 %@股 %@",@(trendModel.buyPriceDeal).r04fString,@(trendModel.volumeDeal),trendModel.stockName];
    
    NSMutableAttributedString *detailAttString = [[NSMutableAttributedString alloc]initWithString:detailString];
    [detailAttString addAttribute:NSForegroundColorAttributeName value:[YZMAppStyle colorForIconColor] range:NSMakeRange(detailAttString.length-trendModel.stockName.length, trendModel.stockName.length)];
    _detailLabel.attributedText = detailAttString;
    _timeLabel.text = [YZMTimeJudge compareCurrentTime:trendModel.tradingTime];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
